<template>
	<div>
		<!--新增界面-->
		<el-dialog v-loading.fullscreen.lock="loading" element-loading-text="处理中......" title="新增条款" :visible.sync="dialogVisible" size="small" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off" :top="dialog_margin_top">
			  <el-form label-width="160px" >
			  <el-row :span="24">
			  	<el-col :span="12">
			  		<el-form-item label="归属险类" required>
	  					<el-select v-model="formData.ownerriskcode" filterable size="small" @change="newKindCode()">
							<el-option v-for="item in classOptions" :key="item.classcode" :label="item.classcode + '-' + item.classname" :value="item.classcode"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="主险/附加险" required>
						<el-select v-model="formData.kindType" filterable size="small" @change="newKindCode()">
							<el-option v-for="item in kindTypeOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<!-- 移除原因：点击保存时才生成条款代码 -->
					<!-- <el-form-item label="条款代码" required>
						<el-input v-model="formData.kindcode" size="small" disabled></el-input>
					</el-form-item> -->
					<el-form-item label="条款中文名称" required>
						<el-input v-model="formData.kindcname" size="small" :maxlength="255"></el-input>
					</el-form-item>
					<el-form-item label="条款英文名称" required>
						<el-input v-model="formData.kindename" size="small" :maxlength="255"></el-input>
					</el-form-item>
					<el-form-item label="条款版本" required>
						<el-select v-model="formData.kindversion" filterable size="small">
							<el-option v-for="item in versionOptions" :key="item.value" :label="item.value" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="算费方式标识" required>
						<el-select v-model="formData.waysofcalc" filterable size="small" @change="handleCalculateType">
							<el-option v-for="item in calculateTypeOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="iLog费率配置状态" required>
						<el-select v-model="formData.ilogflag" :disabled="disabledGroup.iLogStatusFlag" filterable size="small" >
							<el-option v-for="item in iLogFlagOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="短期费率方式" required>
						  <el-select v-model="formData.shortratetype" placeholder="请选择" size="small">
										<el-option v-for="item in shortRateOptions" :key="item.shortratetype" :label="item.shortratetype" :value="item.shortratetype"></el-option>
									</el-select>
			  		</el-form-item>
			  		<el-form-item label="条款性质" required>
						  <el-select v-model="formData.kindNature" placeholder="请选择" size="small">
										<el-option v-for="item in kindNatureOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
									</el-select>
			  		</el-form-item>
					<el-form-item label="备注">
						<el-input type="textarea" v-model="formData.remark" :maxlength="4000" size="small" :rows="2"></el-input>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="12">
					<el-form-item label="固定费率(%)">
						<el-input v-model="formData.agreementrate" :disabled="disabledGroup.appointRateFlag" @keyup.native="checkAgreementRate()" size="small"></el-input>
					</el-form-item>
					<el-form-item label="对应加成条款代码">
						<el-input type="textarea" v-model="formData.pluskindcode" :disabled="disabledGroup.plusKindCodeFlag" size="small" readonly @focus="openKindSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="加成比例(%)">
						<el-input v-model="formData.plusrate" :disabled="disabledGroup.plusRateFlag" @keyup.native="checkPlusRate()" size="small"></el-input>
					</el-form-item>
					<el-form-item label="条款权限机构代码">
						<el-input type="textarea" v-model="comLabel" size="small" readonly @dblclick.native="openCompanySelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="是否配置备案号" required>
						<el-select v-model="formData.efilestatus" filterable size="small" disabled>
							<el-option v-for="item in eFileOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="是否累计保额" required>
						<el-select v-model="formData.calculateflag" filterable size="small">
							<el-option v-for="item in calculateOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="Cas抄单" required>
						<el-select v-model="formData.cas" filterable size="small">
							<el-option v-for="item in casOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="有效标识" required>
						<el-select v-model="formData.validstatus" size="small">
							<el-option v-for="item in validOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="版本生效日期" required>
						<el-date-picker type="date" placeholder="选择日期" v-model="formData.startdate" style="width: 100%;" :editable="false" size="small"></el-date-picker>
					</el-form-item>
					<el-form-item label="版本失效日期" required>
						<el-date-picker type="date" placeholder="选择日期" v-model="formData.enddate" style="width: 100%;" :editable="false" size="small"></el-date-picker>
					</el-form-item>
			  	</el-col>
			  </el-row>
			</el-form>
			  <span slot="footer" class="dialog-footer">
				   <el-button @click="off">取 消</el-button>
				   <el-button type="warning" @click="add">新 增</el-button>
			  </span>
		</el-dialog>
		<KindSelect  v-if="showKindSelectDialog"></KindSelect>
		<ComSelect  v-if="showComSelectDialog"></ComSelect>
	</div>
</template>

<script>

import KindAPI from "../../../../js/production/kind/kindAPI"
import RiskAPI from "../../../../js/production/risk/riskAPI"
import Validations from "../../../../js/production/validations"
import KindSelect from "./KindSelect";
import ComSelect from "../../common/ComSelect";

  export default {
  	components : {
	  	KindSelect,
	  	ComSelect
	  },
	data() {
		return {
			dialogVisible: true,
			dialog_margin_top: '5%',
			formData: {
				kindcname: '',
				kindename: '',
				ownerriskcode: '',
				kindcode: '',
				kindversion: '1.0',
				kindType: '1',
				ilogflag: '0',
				calculateflag: 'N',
				cas: 'M',
				efilestatus: 'N',
				waysofcalc: '0',
				agreementrate: '',
				pluskindcode: '',
				plusrate: '',
				powagentcode: '',
				validstatus: '0',
				remark: '',
				startdate: '',
				enddate: '',
				shortratetype : '',
				kindNature: '0',
			},
			comLabel : '',
			arrayComcodes : [],
			classOptions: [],
			shortRateOptions: [],
			kinds : [],
			versionOptions: [
				{value: '1.0'},
			],
			kindTypeOptions: [
				{label: '主险',  value: '1'},
				{label: '附加险 ',  value: '2'},
			],
			kindNatureOptions: [
				{label: '0-物质损失',  value: '0'},
				{label: '1-营业中断',  value: '1'},
			],
			iLogFlagOptions: [
				{label: '是',  value: '1'},
				{label: '否 ',  value: '0'},
			],
			calculateOptions: [
				{label: '是',  value: 'Y'},
				{label: '否 ',  value: 'N'},
			],
			casOptions: [
				{label: '抄明细',  value: 'M'},
				{label: '抄备注 ',  value: 'B'},
			],
			eFileOptions: [
				{label: '是',  value: 'Y'},
				{label: '否 ',  value: 'N'},
			],
			calculateTypeOptions: [
				{label: '手录保费',value: '0'},
				{label: '固定费率',value: '1'},
				{label: 'iLog算费',value: '2'},
				{label: '加成比例',value: '3'},
			],
			orgOptions: [

			],
			validOptions: [
				{label : '1-有效',value: '1'},
				{label : '0-无效',value: '0'}
			],
			showKindSelectDialog: false,
			showComSelectDialog: false,
			disabledGroup: {
				appointRateFlag : true,
				plusKindCodeFlag : true,
				plusRateFlag : true,
				iLogStatusFlag : true,
			},
			loading : false,
		}
	},
	mounted() {
		KindAPI.initRiskSelect(this);
		RiskAPI.initShortRate(this);
	},
	watch : {
		comLabel(newVal,oldVal) {
			this.formData.powagentcode = newVal;
		}
	},
	methods: {
		checkAgreementRate() {
			Validations.checkDecimal(3, 4, this.formData, "agreementrate");
		},
		checkPlusRate() {
			Validations.checkDecimal(3, 4, this.formData, "plusrate");
		},
		add() {
			if(this.formData.enddate < this.formData.startdate) {
				this.$message({type: 'error',message: '【版本失效日期】不能小于【版本生效日期】'});
				this.formData.enddate = null;
				return;
			}
			KindAPI.add(this);
		},
		off() {
			this.$parent.showAddDialog = false;
		},
		newKindCode() {
			// KindAPI.newKindCode(this);   //移除原因：点击保存时才生成条款代码
		},
		openKindSelectDialog() {
			this.showKindSelectDialog = true;
		},
		openCompanySelectDialog() {
			this.showComSelectDialog = true;
		},
		handleCalculateType() {
			switch(this.formData.waysofcalc) {
				case '0': {
					this.disabledGroup.appointRateFlag = true;
					this.disabledGroup.plusKindCodeFlag = true;
					this.disabledGroup.plusRateFlag = true;
					this.disabledGroup.iLogStatusFlag = true;
					this.formData.agreementrate = '';
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					this.formData.ilogflag = '0';
					break;
				}
				case '1': {
					this.disabledGroup.appointRateFlag = false;
					this.disabledGroup.plusKindCodeFlag = true;
					this.disabledGroup.plusRateFlag = true;
					this.disabledGroup.iLogStatusFlag = true;
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					this.formData.ilogflag = '0';
					break;
				}
				case '2': {
					this.disabledGroup.appointRateFlag = true;
					this.disabledGroup.plusKindCodeFlag = true;
					this.disabledGroup.plusRateFlag = true;
					this.disabledGroup.iLogStatusFlag = false;
					this.formData.agreementrate = '';
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					break;
				}
				case '3': {
					this.disabledGroup.appointRateFlag = true;
					this.disabledGroup.plusKindCodeFlag = false;
					this.disabledGroup.plusRateFlag = false;
					this.disabledGroup.iLogStatusFlag = true;
					this.formData.agreementrate = '';
					this.formData.ilogflag = '0';
				}

			}


		},
	}
  }
</script>
